package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.ScenicSpot;
import cn.wolfcode.trip.base.domain.Theme;
import cn.wolfcode.trip.base.domain.Ticket;
import cn.wolfcode.trip.base.mapper.ScenicSpotMapper;
import cn.wolfcode.trip.base.mapper.ThemeMapper;
import cn.wolfcode.trip.base.mapper.TicketMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IScenicSpotService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ScenicSpotServiceImpl implements IScenicSpotService {
    @Autowired
    private ScenicSpotMapper scenicSpotMapper;
    @Autowired
    private ThemeMapper themeMapper;

    @Autowired
    private TicketMapper ticketMapper;
    @Override
    public PageInfo<ScenicSpot> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<ScenicSpot> list = scenicSpotMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(ScenicSpot entity) {
        //维护门票表的关系
        Ticket ticket=new Ticket();
        //设置价格
        ticket.setPrice(entity.getTicket().getPrice());
        //如果不为空,就是编辑
        if(entity.getId()!=null){
            scenicSpotMapper.updateByPrimaryKey(entity);
            ticket.setId(entity.getTicket().getId());
            ticketMapper.updateTicketPrice(ticket);
        }else{
            //维护ticket表
            Date date=new Date();
            ticket.setDate(date);
            Integer index=(ticketMapper.countMaxId()+1);
            ticketMapper.insert(ticket);
            ticket.setId(index.longValue());
            entity.setTicket(ticket);
            scenicSpotMapper.insert(entity);
            ticket.setScenicSpot(entity);
            ticketMapper.updateByPrimaryKey(ticket);
        }
    }

    @Override
    public List<Theme> listTheme() {
        return themeMapper.selectAll();
    }

    @Override
    public PageInfo<ScenicSpot> getScenicSpotByRegionId(Long regionId,QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<ScenicSpot> list= scenicSpotMapper.getScenicSpotByRegionId(regionId,qo);
        return new PageInfo<ScenicSpot>(list) ;
    }

    @Override
    public Object getscenicSpotById(Long id) {
        return scenicSpotMapper.selectByPrimaryKey(id);
    }


}
